package com.tree;

import utils.CreateArr;

/**
 * 二分查找法:
 */
public class BinarySearch {

    //在数组 arr 中,在区间 [ left , right ] 中查找 target 的下标
    private static int binarySearch(int[] arr, int left, int right, int target) {
        if (left <= right) {
            int middle = left + (right - left) / 2;
            if (arr[middle] == target) {
                return middle;
            } else if (target < arr[middle]) {
                return binarySearch(arr, left, middle - 1, target);
            } else {
                return binarySearch(arr, middle + 1, right, target);
            }
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        int arr = binarySearch(CreateArr.createSortInt(20, 0, 1), 0, 19, 0);
        System.out.print(arr);
    }

}
